<?php
namespace app\admin\controller;
use app\model\KlingaiRecord as KlingaiRecordModel;
use app\model\User as UserModel;
use think\Db;


class KlingaiRecord extends Base
{
    public function __construct(){
        $model = new KlingaiRecordModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '可灵';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignSearchs(){
        $model = $this->model;

        $tmp_users = Db::name('user')->field('id,truename,mobile')->select();
        $users = [];
        foreach ($tmp_users as $k => $vo) {
            $users[$vo['id']] = $vo['truename'].'('.$vo['mobile'].')';
        }

        $search_params = [
            [
                ['key'=>'user_id','name'=>'用户','type'=>'select2_1','params'=>$users],
                ['key'=>'start_time','name'=>'开始时间','type'=>'date'],
                ['key'=>'end_time','name'=>'结束时间','type'=>'date'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function getMap(){
        $map_params = [
            ['key'=>'user_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('start_time')) {
            $map[] = ['ctime','>=',strtotime(input('start_time'))];
        }
        if (isSearchParam('end_time')) {
            $map[] = ['ctime','<=',strtotime(input('end_time'))];
        }

        return $map;
    }

    public function index(){
        $model = $this->model;
        $map = $this->getMap();
        $this->assignSearchs();

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
           $list['data'][$k]['user'] = Db::name('user')->where('id',$vo['user_id'])->find();
           $list['data'][$k]['utype_cn'] = UserModel::getUtypeCn($vo['utype']);
           $list['data'][$k]['ram_account'] = empty($vo['ram_id']) ? "" : Db::name('ram')->where('id',$vo['ram_id'])->value('account');
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    
    public function detail($id,$type){
        $model = $this->model;
        $data = $model->where('id',$id)->find();
        $content = $data[$type];
        return json(['status'=>1,'data'=>$content]);
    }

   
}
